package com.forever.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.forever.entity.User;
import com.forever.user.service.UserDataService;
import com.forever.util.CommonField;
import com.forever.util.CommonUtil;

@Controller
@RequestMapping("/user/*")
@Scope(value = "prototype")
public class UserDataController {

	@Autowired
	private UserDataService userDataService;

	@RequestMapping(value = "/add.html", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView userAdd() {
		String id = CommonUtil.getLuckNumber(CommonField.THREE_INT);
		userDataService.toAdd(id);
		return new ModelAndView("user/user_to_add");
	}

	@RequestMapping(value = "/save.html", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView userSave(@ModelAttribute("user") User user) {
		userDataService.saveUser(user);
		return new ModelAndView("redirect:/user/list.html");
	}

	@RequestMapping(value = "/list.html", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView list() {
		return new ModelAndView("user/user_list", "list", userDataService.getAll());
	}
	
	@RequestMapping(value = "/httl.html", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView httl() {
		return new ModelAndView("httl");
	}
}